package com.qianphone.mylbs;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

public class MyPoiSearchActivity extends FragmentActivity implements OnClickListener,
		OnGetPoiSearchResultListener {
	private EditText cityEdit;
	// 搜索的关键字的编辑框对象
	private EditText keywordEdit;

	private PoiSearch mPoiSearch;

	private BaiduMap mBaiduMap;

	// 点击某个热点后弹出的窗口的内容
	private View infoWindowView;
	private TextView detailTxt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SDKInitializer.initialize(getApplicationContext());

		setContentView(R.layout.activity_poisearch);

		infoWindowView = findViewById(R.id.pop_container);
		detailTxt = (TextView) infoWindowView.findViewById(R.id.detail_txt);

		cityEdit = (EditText) findViewById(R.id.city_edit);
		keywordEdit = (EditText) findViewById(R.id.searchkey_edit);

		View searchBtn = findViewById(R.id.search_btn);
		View nextPageBtn = findViewById(R.id.map_next_data_btn);

		searchBtn.setOnClickListener(this);
		nextPageBtn.setOnClickListener(this);

		// 创建一个热点搜索的对象
		mPoiSearch = PoiSearch.newInstance();
		// 给Poisearch对象设置搜索结果的回调监听器对象
		mPoiSearch.setOnGetPoiSearchResultListener(this);

		// 找到地图Fragment对象
		SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map_fragment);

		mBaiduMap = fragment.getBaiduMap();

	}

	private int pageIndex;

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.search_btn:// 开始搜索
			poiSearch(pageIndex);

			break;
		case R.id.map_next_data_btn:// 下一页的数据
			pageIndex++;
			poiSearch(pageIndex);
			break;

		default:
			break;
		}
	}

	private void poiSearch(int pageIndex) {
		String city = cityEdit.getText().toString();
		String keyword = keywordEdit.getText().toString();
		PoiCitySearchOption option = new PoiCitySearchOption();
		option.city(city);
		option.keyword(keyword);
		option.pageNum(pageIndex);
		mPoiSearch.searchInCity(option);
	}

	/**
	 * 当某一个热点的详情数据返回的时候被baiduSDK调用
	 */
	@Override
	public void onGetPoiDetailResult(PoiDetailResult result) {
		// System.out.println("服务评分 = " + result.getServiceRating() + " 环境评分 = "
		// + result.getEnvironmentRating() + " 设备评分 = " +
		// result.getFacilityRating()
		// + " 价格 = " + result.getPrice() + " 地址 = " + result.getAddress() +
		// " 名字 = "
		// + result.getName());
		String detail = "服务评分 = " + result.getServiceRating() + " 环境评分 = "
				+ result.getEnvironmentRating() + " 设备评分 = " + result.getFacilityRating()
				+ " 价格 = " + result.getPrice() + " 地址 = " + result.getAddress() + " 名字 = "
				+ result.getName();

		detailTxt.setText(detail);

	}

	/**
	 * 当某一组poi数据返回时被baiduSDK调用
	 */
	@Override
	public void onGetPoiResult(PoiResult result) {
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			// List<PoiInfo> infos = result.getAllPoi();
			// for (PoiInfo poiInfo : infos) {
			// System.out.println(poiInfo.address + " ; name =  " + poiInfo.name
			// + " ; phoneNum = " + poiInfo.phoneNum + " ; postCode = " +
			// poiInfo.postCode
			// + " ; uid = " + poiInfo.uid + " ; hasCaterDetails = "
			// + poiInfo.hasCaterDetails + " ; type = " + poiInfo.type);
			// }

			// 创建一个显示热点搜索结果的覆盖物
			PoiOverlay poiOverlay = new MyPoiOverlay(mBaiduMap);

			// 给覆盖物设置热点数据
			poiOverlay.setData(result);
			// 让覆盖物显示到地图上
			poiOverlay.addToMap();
			// 把地图缩放到所有覆盖物能够显示的范围内
			poiOverlay.zoomToSpan();

			// // 把覆盖物设置成为marker点击的监听器，当覆盖物上某个marker被点击的时候，会得到具体的详情
			mBaiduMap.setOnMarkerClickListener(poiOverlay);

		}
		// System.out.println("onGetPoiResult = " + result.toString());
	}

	@Override
	public void onBackPressed() {
		if (infoWindowView.getVisibility() == View.VISIBLE) {
			infoWindowView.setVisibility(View.GONE);
		} else {
			finish();
		}
	}

	private class MyPoiOverlay extends PoiOverlay {

		public MyPoiOverlay(BaiduMap arg0) {
			super(arg0);
		}

		/**
		 * 当地图上显示的热点被点击的时候会被调用
		 * 
		 * @index 当前poi列表中的第几个热点被点击
		 */
		@Override
		public boolean onPoiClick(int index) {

			// 获取到当前正在显示的poi列表,并且从列表中取出index位置上的那个数据再次发起详情请求
			List<PoiInfo> infos = getPoiResult().getAllPoi();
			PoiInfo poiInfo = infos.get(index);

			// 创建一个Poi详情请求的对象
			PoiDetailSearchOption detailSearchOption = new PoiDetailSearchOption();
			// 设置请求的是那一个poi热点的UID
			detailSearchOption.poiUid(poiInfo.uid);

			// 把详情请求发送给服务器
			mPoiSearch.searchPoiDetail(detailSearchOption);

			infoWindowView.setVisibility(View.VISIBLE);
			return true;
		}
	}
}